﻿using AutoMapper;
using Hsp.WebNovels.Model.Models;
using Hsp.WebNovels.Web.Utilities;
using Microsoft.AspNetCore.Mvc;

namespace Hsp.WebNovels.Web.Controllers
{
    public class ContentController : BaseController
    {
        #region 控制器初始化

        private readonly IMapper _mapper;
        private readonly ILogger<Controller> _logger;
        private readonly NovelsContext _context;
        private readonly IWebHostEnvironment _env;

        /// <summary>
        /// 章节内容控制器构造函数
        /// </summary>
        public ContentController(ILogger<ContentController> logger, NovelsContext context, IMapper mapper, IWebHostEnvironment env) : base(logger, context, mapper)
        {
            this._logger = logger;
            this._mapper = mapper;
            this._context = context;
            _env = env; 
        }

        #endregion

        #region 小说内容清空

        /// <summary>
        /// 小说内容清空：可清空单个，多个或者全部
        /// GET: Content/Clear
        /// </summary>
        /// <param name="id">章节ID/章节ID集合</param>
        /// <returns></returns>
        public async Task<IActionResult> Clear(string? id)
        {
            try
            {
                #region 全部清空

                if (string.IsNullOrEmpty(id))
                {
                    throw new Exception("章节ID为空");
                }

                #endregion

                #region 部分清空

                if (!string.IsNullOrEmpty(id) && id.Contains(','))
                {
                    id = id.Replace(",", "','");
                    var strSql = string.Format(@"DELETE FROM [dbo].[Contents] WHERE (ChapterId IN ('{0}'));", id);
                    await Db.Ado.ExecuteCommandAsync(strSql);
                }

                #endregion

                #region 单个清空

                if (!string.IsNullOrEmpty(id) && !id.Contains(','))
                {
                    await Db.Deleteable<Content>(id).ExecuteCommandAsync();
                }

                #endregion

                return Json(new { success = true, message = "小说内容清空成功" });
            }
            catch (Exception ex)
            {
                _logger.LogError($"{nameof(Clear)}：{ex}");
                return Json(new { success = false, message = ex.Message });
            }
        }

        #endregion






    }
}
